package org.ifdb.controller;

import org.ifdb.persistency.PMF;
import org.ifdb.persistency.entity.City;
import org.ifdb.persistency.entity.Country;
import org.ifdb.persistency.entity.State;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IfdbController {

    @RequestMapping(value = "/ifdb.do")
    public void get() {

    }

    @RequestMapping(value = "/ifdb.do", method = RequestMethod.POST)
    public void get(String name, Model model) {
        model.addAttribute("name", name);
    }

    /***
     * testing Spring and JSON (consuming with jquery)
     * 
     * @return
     */
    @RequestMapping(value = "/ifdb/test.do")
    public @ResponseBody Country test(@RequestParam String name) {
        City city = new City();
        city.setName("Sorocaba");
        State state = new State();
        state.setName("SP");
        Country country = new Country();
        country.setName("Brasil");

        state.addCity(city);
        country.addState(state);
        PMF.get().getPersistenceManager().makePersistent(country);
        return country;

    }
}
